﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Web.Script.Serialization;
using ihongma.Common;

namespace ihongma.Web.tools
{
    /// <summary>
    /// website 的摘要说明
    /// </summary>
    public class website : IHttpHandler
    {
        BLL.webcase web = new BLL.webcase();
        public void ProcessRequest(HttpContext context)
        {
            
            context.Response.ContentType = "text/plain";
            int pagesize = 12;
            int pageindex = 1;
            string strwhere = "id>0";
            int recordCount = 0;
            if (context.Request.QueryString["pageindex"] != null)
            {
                pageindex = int.Parse(context.Request.QueryString["pageindex"].ToString());
            }
            if (context.Request.QueryString["color"] != "")
            {
                strwhere =  "  color like '%" + context.Request.QueryString["color"].ToString() + "%'";
            }
            if (context.Request.QueryString["tag"] != "")
            {
                strwhere =  "  tags like '%" + context.Request.QueryString["tag"].ToString() + "%'";
            }
            if (context.Request.QueryString["name"] != "")
            {
                strwhere =  "  name like '%" + context.Request.QueryString["name"].ToString() + "%'";
            }
            DataSet DT = web.GetList(pagesize, pageindex, strwhere, "id desc", out  recordCount);
            returlist LIST = new returlist();
            LIST.list = web.DataTableToList(DT.Tables[0]);
            for (int i = 0; i < LIST.list.Count; i++)
            {
                LIST.list[i].tags = tags(LIST.list[i].tags);
            }
            LIST.pagecount = recordCount;

            JavaScriptSerializer jserializer = new JavaScriptSerializer();
            context.Response.Write(jserializer.Serialize(LIST));
            //context.Response.Write("Hello World");
        }
        public string tags(string id)
        {
            JavaScriptSerializer jserializer = new JavaScriptSerializer();
            return jserializer.Serialize(new BLL.Tags().GetModelList("id in (" + id + ")"));
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
        public class returlist
        {
            public List<Model.webcase> list
            {

                set;
                get;
            }
            public int pagecount
            {
                set;
                get;
            }
        }
    }
}